<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-cascades"></i> 基础表格
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
          <el-button type="primary" icon="el-icon-edit" @click="handleAddConsumer"  style='position: absolute;right:100px;top:80px;'>添加</el-button>
            <div class="handle-box">
                <!-- <el-input v-model="query.name" placeholder="用户名" class="handle-input mr10"></el-input>
                <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button> -->
            </div>
            <el-table
                :data="PostsData"
                border
                class="table"
                ref="multipleTable"
                header-cell-class-name="table-header"
                @selection-change="handleSelectionChange"
            >
                <el-table-column type="selection" width="55" align="center"></el-table-column>
                <el-table-column prop="pid" label="岗位编号" width="55" align="center"></el-table-column>
                <el-table-column prop="pname" label="岗位名称"></el-table-column>
				        <el-table-column prop="ptype" label="类型"></el-table-column>
                <el-table-column prop="pweave" label="岗位编制"></el-table-column>
                <el-table-column label="操作" width="180" align="center">
                    <template slot-scope="scope">
                        <el-button
                            type="text"
                            icon="el-icon-edit"
                            @click="handleEdit(scope.$index, scope.row)"
                        >编辑</el-button>
                        <el-button
                            type="text"
                            icon="el-icon-delete"
                            class="red"
                            @click="handleDelete(scope.row)"
                        >删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                    background
                    layout="total, prev, pager, next"
                    :current-page="query.pageIndex"
                    :page-size="query.pageSize"
                    :total="pageTotal"
                    @current-change="handlePageChange"
                ></el-pagination>
            </div>
        </div>

        <!-- 编辑弹出框 -->
        <el-dialog title="编辑" :visible.sync="editVisible" width="30%">
            <el-form ref="form" :model="form" label-width="70px">
                <el-form-item label="岗位编号">
                    <el-input v-model="partsData.pid"></el-input>
                </el-form-item>
				        <el-form-item label="岗位名称">
                    <el-input v-model="partsData.pname"></el-input>
                </el-form-item>
				        <el-form-item label="类型">
                    <el-input v-model="partsData.ptype"></el-input>
                </el-form-item>
				        <el-form-item label="岗位编制">
                    <el-input v-model="partsData.pweave"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="editVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveEdit">确 定</el-button>
            </span>
        </el-dialog>
      <!-- 添加弹出框 -->
      <el-dialog title="添加" :visible.sync="addVisible" width="30%">
        <el-form ref="form" :model="form" label-width="70px">
          <el-form-item label="岗位编号">
            <el-input v-model="tempcustomerData.pid"></el-input>
          </el-form-item>
          <el-form-item label="岗位名称">
            <el-input v-model="tempcustomerData.pname"></el-input>
          </el-form-item>
          <el-form-item label="类型">
            <el-input v-model="tempcustomerData.ptype"></el-input>
          </el-form-item>
          <el-form-item label="岗位编制">
            <el-input v-model="tempcustomerData.pweave"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
                <el-button @click="addVisible = false">取 消</el-button>
                <el-button type="primary" @click="Addconsumer">确 定</el-button>
            </span>
      </el-dialog>
    </div>
</template>

<script>
import { fetchPostsData,fetchupdatePosts,fetchaddPosts,Postsdelete} from '../../api/index';
export default {
    name: 'basetable',
    data() {
        return {
            PostsData: [],
            tempcustomerData:{},
            multipleSelection: [],
            delList: [],
            Postsdelete:{},
            partsData:{},
            query: {},
            editVisible: false,
            addVisible:false,
            pageTotal: 0,
            form: {},
            idx: -1,
            id: -1
        };
    },
    created() {
        this.getPostsData();
    },
    methods: {
      getPostsData() {
        fetchPostsData().then(res =>{console.info(res)
          this.PostsData=res.data.value
        })
      },
        // 触发搜索按钮
        handleSearch() {
            this.$set(this.query, 'pageIndex', 1);
            this.getPostsData();
        },
        // // 删除操作
        handleDelete(row) {
        this.Postsdelete = row;
        Postsdelete(this.Postsdelete).then(res=>{console.info(res)
          console.log(this.Postsdelete)
          console.log(res.code)
          if (res.code == 20000)
          { 
            window.location.reload()
            this.$message.success(`删除成功`);
          } 
          else
          {
            this.$message.success('该岗位下已有员工，不能删除!');  
          }
         
        })
      },
        // 编辑操作
        handleEdit(index,row) {
            this.idx = index;
            this.partsData = row;
            this.editVisible = true;
        },
        // 保存编辑
        saveEdit() {
        this.editVisible = false;
        this.$message.success(`修改第 ${this.idx + 1} 行成功`);
          fetchupdatePosts(this.partsData).then(res =>{console.info(res)
          console.log(this.partsData)
          console.log(res)
        })
      },
      //添加函数
      handleAddConsumer(){
        this.addVisible = true;
      },
      Addconsumer() {
        this.addVisible = false;
        this.$message.success(`添加成功`);
        fetchaddPosts(this.tempcustomerData).then(res =>{console.info(res)
          console.log(this.tempcustomerData)
          console.log(res)
          window.location.reload()
        })
      },
        // 分页导航
        handlePageChange(val) {
            this.$set(this.query, 'pageIndex', val);
            this.getData();
        }
    }
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}

.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
</style>
